package com.izhengyin.gateway.webagent;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhengyin zhengyinit@outlook.com
 * Created on 2020-09-19 13:58
 */

public class GatewayWebAgentService implements ApplicationContextAware {
    private ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    /**
     * 获取服务的API定义描述
     * @return
     */
    List<GatewayWebApiDefine> getApiDefines(){
       ApiDefineProvider apiDefineProvider;
        try {
            Class.forName("org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping");
            apiDefineProvider = new SpringWebfluxApiDefineProvider();
        }catch (ClassNotFoundException e){
            apiDefineProvider = new SpringMvcApiDefineProvider();
        }
        return apiDefineProvider.getApiDefines(context)
                .stream()
                .map(api -> {
                    //去掉方法的参数列表中的 [ ( , ) 空格 ] , 转换为可配置的字符
                    api.setMethod(api.getMethod()
                            .replace("#","-")
                            .replace("(","-")
                            .replace(" ","")
                            .replace(",","-")
                            .replace(")","")
                    );
                    return api;
                })
                .collect(Collectors.toList());
    }

}
